package com.huashi.dealer.modular.business.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huashi.dealer.commonEntity.ArticleCategory;
import com.huashi.dealer.core.exception.CommonException;
import com.huashi.dealer.modular.business.article.mapper.ArticleCategoryMapper;
import com.huashi.dealer.modular.business.article.service.ArticleCategoryService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * API文章分类Service实现类
 *
 * @author 谭陈强
 * @since 2025-07-30
 */
@Service("apiArticleCategoryServiceImpl")
public class ArticleCategoryServiceImpl extends ServiceImpl<ArticleCategoryMapper, ArticleCategory> implements ArticleCategoryService {

    @Resource
    private ArticleCategoryMapper articleCategoryMapper;
    @Autowired
    private ArticleCategoryService articleCategoryService;

    @Override
    public List<ArticleCategory> getAllCategories() {
        try {
            LambdaQueryWrapper<ArticleCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByAsc(ArticleCategory::getSort);
            return this.list(queryWrapper);
        } catch (Exception e) {
            throw new CommonException("获取分类列表失败：" + e.getMessage());
        }
    }

    @Override
    public List<ArticleCategory> getCategoriesByType(Integer isType) {
        try {
            LambdaQueryWrapper<ArticleCategory> queryWrapper = new LambdaQueryWrapper<>();
            if (isType != null) {
                // 注意：这里需要根据实际的数据库字段进行调整
                // 如果ArticleCategory实体类中有isType字段，则使用该字段
                // 如果没有，可能需要通过关联查询或其他方式实现
                // queryWrapper.eq(ArticleCategory::getIsType, isType);
            }
            queryWrapper.orderByAsc(ArticleCategory::getSort);
            return this.list(queryWrapper);
        } catch (Exception e) {
            throw new CommonException("根据类型获取分类列表失败：" + e.getMessage());
        }
    }

    @Override
    public List<ArticleCategory> getCategoriesByWxappId(Integer wxappId) {
        try {
            LambdaQueryWrapper<ArticleCategory> queryWrapper = new LambdaQueryWrapper<>();
            if (wxappId != null) {
                queryWrapper.eq(ArticleCategory::getWxappId, wxappId);
            }
            queryWrapper.orderByAsc(ArticleCategory::getSort);
            return this.list(queryWrapper);
        } catch (Exception e) {
            throw new CommonException("根据小程序ID获取分类列表失败：" + e.getMessage());
        }
    }

    @Override
    public ArticleCategory getCategoryById(Integer categoryId) {
        try {
            if (categoryId == null) {
                throw new CommonException("分类ID不能为空");
            }
            return this.getById(categoryId);
        } catch (Exception e) {
            throw new CommonException("获取分类信息失败：" + e.getMessage());
        }
    }


    @Override
    public List<ArticleCategory> getCategoriesOrderBySort() {
        try {
            LambdaQueryWrapper<ArticleCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.orderByAsc(ArticleCategory::getSort);
            return this.list(queryWrapper);
        } catch (Exception e) {
            throw new CommonException("获取排序分类列表失败：" + e.getMessage());
        }
    }
} 